﻿<!-- Views/Admin/Users.cshtml - 简化美化版 -->

@model List<WebApplication1.Models.User>

@{
    ViewBag.Title = "用户管理";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<div class="container">
    <h2 class="mb-4">
        <i class="glyphicon glyphicon-user"></i> 用户管理
    </h2>
    <hr />

    <!-- 消息提示 -->
    @if (TempData["SuccessMessage"] != null)
    {
        <div class="alert alert-success alert-dismissible fade show" role="alert">
            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                <span aria-hidden="true">&times;</span>
            </button>
            <i class="glyphicon glyphicon-ok-circle"></i> @Html.Raw(TempData["SuccessMessage"])
        </div>
    }

    @if (TempData["ErrorMessage"] != null)
    {
        <div class="alert alert-danger alert-dismissible fade show" role="alert">
            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                <span aria-hidden="true">&times;</span>
            </button>
            <i class="glyphicon glyphicon-exclamation-sign"></i> @Html.Raw(TempData["ErrorMessage"])
        </div>
    }

    <!-- 操作按钮 -->
    <div class="mb-3">
        <a href="@Url.Action("CreateUser", "Admin")" class="btn btn-primary">
            <i class="glyphicon glyphicon-plus"></i> 添加用户
        </a>
        <a href="@Url.Action("ImportEmployees", "Admin")" class="btn btn-success">
            <i class="glyphicon glyphicon-import"></i> 导入员工
        </a>
    </div>

    <!-- 用户列表 -->
    <div class="card">
        <div class="card-header bg-primary text-white">
            <h5 class="mb-0">
                <i class="glyphicon glyphicon-list"></i> 用户列表
            </h5>
        </div>
        <div class="card-body">
            @if (Model == null || Model.Count == 0)
            {
                <div class="alert alert-info text-center">
                    <i class="glyphicon glyphicon-info-sign"></i>
                    <h4>暂无用户信息</h4>
                    <p>点击上方"添加用户"按钮创建新用户</p>
                </div>
            }
            else
            {
                <div class="table-responsive">
                    <table class="table table-striped table-bordered table-hover">
                        <thead class="thead-dark">
                            <tr>
                                <th class="text-center">ID</th>
                                <th>用户名</th>
                                <th>姓名</th>
                                <th>角色</th>
                                <th>部门</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach (var user in Model)
                            {
                                <tr>
                                    <td class="text-center">@user.UserId</td>
                                    <td>@user.Username</td>
                                    <td>@user.FullName</td>
                                    <td>
                                        @if (user.Role == "Admin")
                                        {
                                            <span class="label label-danger">管理员</span>
                                        }
                                        else
                                        {
                                            <span class="label label-info">员工</span>
                                        }
                                    </td>
                                    <td>
                                        @if (!string.IsNullOrEmpty(user.Department))
                                        {
                                            @user.Department
                                        }
                                        else
                                        {
                                            <span class="text-muted">-</span>
                                        }
                                    </td>
                                    <td>
                                        <a href="@Url.Action("EditUser", "Admin", new { id = user.UserId })"
                                           class="btn btn-warning btn-sm">
                                            <i class="glyphicon glyphicon-edit"></i> 编辑
                                        </a>
                                        <a href="@Url.Action("ResetPassword", "Admin", new { id = user.UserId })"
                                           class="btn btn-info btn-sm">
                                            <i class="glyphicon glyphicon-lock"></i> 重置密码
                                        </a>
                                        @if (user.UserId != (int?)Session["UserId"])
                                        {
                                            <a href="@Url.Action("DeleteUser", "Admin", new { id = user.UserId })"
                                               class="btn btn-danger btn-sm delete-user"
                                               data-username="@user.Username">
                                                <i class="glyphicon glyphicon-trash"></i> 删除
                                            </a>
                                        }
                                        else
                                        {
                                            <button class="btn btn-default btn-sm" disabled>
                                                <i class="glyphicon glyphicon-trash"></i> 删除
                                            </button>
                                        }
                                    </td>
                                </tr>
                            }
                        </tbody>
                    </table>
                </div>
            }
        </div>
    </div>
</div>

<style>
    .card {
        border-radius: 8px;
        box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        border: none;
    }

    .card-header {
        border-radius: 8px 8px 0 0 !important;
        font-weight: 500;
    }

    .table th {
        font-weight: 600;
        vertical-align: middle;
    }

    .table td {
        vertical-align: middle;
    }

    .btn {
        margin-right: 5px;
        margin-bottom: 5px;
    }

    .label {
        display: inline-block;
        padding: 0.25em 0.6em;
        font-size: 75%;
        font-weight: 700;
        line-height: 1;
        text-align: center;
        white-space: nowrap;
        vertical-align: baseline;
        border-radius: 0.25rem;
    }

    .label-danger {
        background-color: #d9534f;
    }

    .label-info {
        background-color: #5bc0de;
    }

    .table-hover tbody tr:hover {
        background-color: rgba(0,0,0,0.05);
    }

    /* 响应式调整 */
    @@media (max-width: 768px) {
        .table-responsive {
            font-size: 0.9rem;
        }

        .btn-sm {
            padding: 0.25rem 0.5rem;
            font-size: 0.8rem;
        }
    }
</style>

@section Scripts {
    <script>
        $(document).ready(function () {
            // 删除确认
            $('.delete-user').click(function (e) {
                e.preventDefault();
                var username = $(this).data('username');
                var url = $(this).attr('href');

                if (confirm(`确定要删除用户 "${username}" 吗？此操作不可恢复！`)) {
                    window.location.href = url;
                }
            });

            // 自动隐藏消息提示
            setTimeout(function () {
                $('.alert').fadeOut('slow');
            }, 5000);
        });
    </script>
}